var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
prefService.setIntPref( 'extensions.Ultima.Show_Operation_Type' , 3 );
prefService.setIntPref( 'extensions.Ultima.Show_Message_Type'   , 2 );


function Populate_Operationss() {

var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
var Operations_List = prefService.getCharPref( 'extensions.Ultima.Operations_List' ).split('|');

var OPS_Type = prefService.getIntPref( 'extensions.Ultima.Show_Operation_Type' );
var MSG_Type = prefService.getIntPref( 'extensions.Ultima.Show_Message_Type'   );

var temp  = "";
var temp2 = "";
var temp5 = "";

if ( OPS_Type == 0 ) temp2 = "Self Spell";
if ( OPS_Type == 1 ) temp2 = "Sorcery";
if ( OPS_Type == 2 ) temp2 = "Thievery";

if ( MSG_Type == 0 ) temp5 = "Success";
if ( MSG_Type == 1 ) temp5 = "failed";

for (q=1; q<Operations_List.length; q++) {

temp = Operations_List[q].split('#');

     if (( OPS_Type == 3    )&&( MSG_Type == 2    )) Create_Operations(temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6]); // Show All
else if (( OPS_Type == 3    )&&( temp5 == temp[5] )) Create_Operations(temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6]); // Show All Operations
else if (( temp2 == temp[2] )&&( MSG_Type == 2    )) Create_Operations(temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6]); // Show Both
else if (( temp2 == temp[2] )&&( temp5 == temp[5] )) Create_Operations(temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6]);

}

}


function Clear_Operationss() {

var Tree_Children_Operations = document.getElementById( 'Tree_Children_Operations' );

if (Tree_Children_Operations) {

var Item = Tree_Children_Operations.getElementsByTagName( 'treeitem' );

if (Item && Item.length > 0) for(q=Item.length-1; q>=0; q--) Tree_Children_Operations.removeChild( Item[q] );

}

}


function Create_Operations( Row_Num , Date , Type , Operation , Target , Result , Message ) {

var new_item = "";
var new_cell = "";
var new_rows = "";

new_item = document.createElement('treeitem'); new_item.setAttribute( 'id'    , Operation );

new_rows = document.createElement('treerow' );

new_cell = document.createElement('treecell'); new_cell.setAttribute( 'label' , Row_Num   ); new_rows.appendChild( new_cell );
new_cell = document.createElement('treecell'); new_cell.setAttribute( 'label' , Date      ); new_rows.appendChild( new_cell );
new_cell = document.createElement('treecell'); new_cell.setAttribute( 'label' , Type      ); new_rows.appendChild( new_cell );
new_cell = document.createElement('treecell'); new_cell.setAttribute( 'label' , Operation ); new_rows.appendChild( new_cell );
new_cell = document.createElement('treecell'); new_cell.setAttribute( 'label' , Target    ); new_rows.appendChild( new_cell );
new_cell = document.createElement('treecell'); new_cell.setAttribute( 'label' , Result    ); new_rows.appendChild( new_cell );
new_cell = document.createElement('treecell'); new_cell.setAttribute( 'label' , Message   ); new_rows.appendChild( new_cell );

new_item.appendChild(new_rows);

document.getElementById( 'Tree_Children_Operations' ).appendChild( new_item );

}


function Update_Buttons() {

var selected = 0;

var start = new Object();
var end = new Object();

var Current_Tree = document.getElementById("Operations_Tree");
var Num_Ranges = Current_Tree.view.selection.getRangeCount();

for (var tt = 0; tt < Num_Ranges && selected < 2; tt++) {
Current_Tree.view.selection.getRangeAt(tt,start,end);
for (var itemIndex = start.value; itemIndex <= end.value && selected < 2; itemIndex++) selected++;
}

if (selected == 1) {


document.getElementById( "Operations_Copy_Button"     ).disabled = false;
document.getElementById( "Operations_Copy_Row_Button" ).disabled = false;
document.getElementById( "Operations_Remove_Button"   ).disabled = false;

} else {

document.getElementById( "Operations_Copy_Button"     ).disabled = true;
document.getElementById( "Operations_Copy_Row_Button" ).disabled = true;
document.getElementById( "Operations_Remove_Button"   ).disabled = true;

}

var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
if ( prefService.getCharPref( 'extensions.Ultima.Operations_List' ) != "" ) document.getElementById( "Operations_Remove_All_Button" ).disabled = false ;


}


function Remove_Operations() {

var start = new Object();
var end = new Object();

var Current_Tree = document.getElementById( "Operations_Tree" );
var Num_Ranges = Current_Tree.view.selection.getRangeCount();

for (var ww = Num_Ranges - 1; ww >= 0; ww--) {

Current_Tree.view.selection.getRangeAt(ww,start,end);

for (var qq = end.value; qq >= start.value; qq--) {

item = Current_Tree.view.getItemAtIndex(qq);
item.parentNode.removeChild(item);

Name = item.firstChild.firstChild.getAttribute( "label" );
Remove_Operations_Entry( Name );

}

}

}


function Remove_Operations_Entry( Name ) {

var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
var Operations_List = prefService.getCharPref( 'extensions.Ultima.Operations_List' ).split('|');

for(q=0; q<Operations_List.length; q++) {

var entry = Operations_List[q].split('#');

if ( Name == entry[0] ) {

Operations_List.splice(q,1);

Temp_Entry = Operations_List.join('|');
prefService.setCharPref( 'extensions.Ultima.Operations_List' , Temp_Entry );

}

}



Operations_List = prefService.getCharPref( 'extensions.Ultima.Operations_List' ).split('|');

for(q=0; q<Operations_List.length; q++) {

var entry = Operations_List[q].split('#');

entry[0] = q;

Operations_List[q] = entry.join('#');

}

Temp_Entry = Operations_List.join('|');
prefService.setCharPref( 'extensions.Ultima.Operations_List' , Temp_Entry );

Clear_Operationss();
Populate_Operationss();

}






function Remove_All_Operations() {

var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
prefService.setCharPref( 'extensions.Ultima.Operations_List' , "" );

document.getElementById( "Operations_Remove_All_Button" ).disabled = true ;

Clear_Operationss();
Populate_Operationss();

}














function Copy_Operations_To_Clipboard(copytext) {

var str = null;
var trans = null;
var clip = null;
var clipid = null;

str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString); 
if (!str) return false; 
 
str.data = copytext; 
 
trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable); 
if (!trans) return false; 
 
trans.addDataFlavor("text/unicode"); 
trans.setTransferData("text/unicode", str, copytext.length * 2); 
 
clipid = Components.interfaces.nsIClipboard; 
clip = Components.classes["@mozilla.org/widget/clipboard;1"].getService(clipid); 
if (!clip) return false; 
 
clip.setData(trans, null, clipid.kGlobalClipboard); 
copytext = null;
str = null;
trans = null;
clip = null;
clipid = null
return false;

}


function Copy_Operations() {

var Tree_Operations = document.getElementById( 'Operations_Tree' );
var Operations_Message = Tree_Operations.view.getCellText( Tree_Operations.currentIndex , Tree_Operations.columns.getNamedColumn( 'Head_Message' ) );

Copy_Operations_To_Clipboard( Operations_Message );

}


function Copy_All_Operations() {

var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
var Operations_List = prefService.getCharPref( 'extensions.Ultima.Operations_List' ).split('|');

var temp = "";
var List2copy = "";

for (q=1; q<Operations_List.length; q++) {
temp = Operations_List[q].split('#');
List2copy += temp[6] + String.fromCharCode(13,10);
}

Copy_Operations_To_Clipboard( List2copy );

}


function Copy_Row_Operations() {

var Tree_Operations = document.getElementById( 'Operations_Tree' );

var Operations_Row_Num   = Tree_Operations.view.getCellText( Tree_Operations.currentIndex , Tree_Operations.columns.getNamedColumn( 'Head_Row_Num'   ) );
var Operations_Date      = Tree_Operations.view.getCellText( Tree_Operations.currentIndex , Tree_Operations.columns.getNamedColumn( 'Head_Date'      ) );
var Operations_Type      = Tree_Operations.view.getCellText( Tree_Operations.currentIndex , Tree_Operations.columns.getNamedColumn( 'Head_Type'      ) );
var Operations_Operation = Tree_Operations.view.getCellText( Tree_Operations.currentIndex , Tree_Operations.columns.getNamedColumn( 'Head_Operation' ) );
var Operations_Target    = Tree_Operations.view.getCellText( Tree_Operations.currentIndex , Tree_Operations.columns.getNamedColumn( 'Head_Target'    ) );
var Operations_Result    = Tree_Operations.view.getCellText( Tree_Operations.currentIndex , Tree_Operations.columns.getNamedColumn( 'Head_Result'    ) );
var Operations_Message   = Tree_Operations.view.getCellText( Tree_Operations.currentIndex , Tree_Operations.columns.getNamedColumn( 'Head_Message'   ) );

var Operations_Row = Operations_Row_Num + "	" + Operations_Date + "	" + Operations_Type + "	" + Operations_Operation + "	" + Operations_Target + "	" + Operations_Result + "	" + Operations_Message ;

Copy_Operations_To_Clipboard( Operations_Row );

}


function Copy_All_Row_Operations() {

var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
var Operations_List = prefService.getCharPref( 'extensions.Ultima.Operations_List' ).split('|');

var temp = "";
var List2copy = "";

for (q=1; q<Operations_List.length; q++) {
temp = Operations_List[q].split('#');
List2copy += temp[0] + "	" + temp[1] + "	" + temp[2] + "	" + temp[3] + "	" + temp[4] + "	" + temp[5] + "	" + temp[6] + String.fromCharCode(13,10);
}

Copy_Operations_To_Clipboard( List2copy );

}


function Show_Message_Type(Val){

var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
prefService.setIntPref( 'extensions.Ultima.Show_Message_Type' , Val );

Clear_Operationss();
Populate_Operationss();

}


function Show_Operation_Type(Val){

var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
prefService.setIntPref( 'extensions.Ultima.Show_Operation_Type' , Val );

Clear_Operationss();
Populate_Operationss();

}

